<?php    
  $deck = new Deck($_GET['deckId']);

  echo '<h1 id="collection_title">'.$deck->getName().' <img height="15" src="images/edit.jpg" valign="top" onclick="create_textbar(this, '.$deck->getID().')"></h1>';
  echo '<h2 id="collection_description">'.$deck->getDescription().' <img height="15" src="images/edit.jpg" valign="top" onclick="create_textbar(this, '.$deck->getID().')"></h2>';
  echo '<h2>Visibility: '.$deck->getVisibility().' <img height="15" src="images/edit.jpg" valign="top"></h2>';
  echo '<div id="card_container"></div>';

  $cumelativeManaCost = $deck -> get_mana_distribution();
  $manacost = $deck -> get_color_distribution();
  $types = $deck -> get_type_distribution();

  $total_manacost = array_sum($manacost);
  $total_types = array_sum($types);

  $radius = 150;

  echo '
    <canvas id="colour_distribution" height="250" width="400"></canvas>
    <canvas id="type_distribution" height="250" width="400"></canvas>
    <canvas id="manacost_distribution" height="250" width="400"></canvas>

    <script>
      var colour_distribution = new KiwiPieChart(\'colour_distribution\');      
        
      var colourValues = Array();
      var colourTheme = Array();';

      $start = 0;
      $a = 0;
      
      array_multisort($manacost, SORT_DESC);
      
      if($total_manacost > 0)
      {
        foreach($manacost as $symbol => $value)
        {
          
          if($manacost[$symbol]/$total_manacost == 0)
          {
            continue;
          }

          echo '
            colourValues["'.$symbol.'"] = '.$manacost[$symbol].';
            colourTheme['.$a.'] = "'.$symbol.'";
          ';
          $a++;
        }
      }
  echo '</script><script>';
      
      
  echo '
      colour_distribution.setPlotData("Colour distribution", colourValues);
      colour_distribution.setPieColorwheel("manual", colourTheme);
      colour_distribution.setTitle(\'Color distribution\', 35);

      var kiwipiechart = new KiwiPieChart(\'type_distribution\');

      var types = new Array();
      ';

      foreach($types as $typename => $amount)
      {
        echo '
          types[\''.$typename.'\'] = '.$amount.';
        ';
      }

      echo '
        kiwipiechart.setPieColorwheel("grayscale");
        kiwipiechart.setPlotData(\'type distribution\', types);
        kiwipiechart.setTitle(\'Type distribution\', 35);';

  echo '
      var mana_distribution = new KiwiBarChart(\'manacost_distribution\');
      mana_distribution.setTitle(\'Mana distribution\');
      
      var cmc_distribution = new Array();
      ';
      
      foreach($cumelativeManaCost as $CMC => $total)
      {
        echo '
          cmc_distribution['.$CMC.'] = '.$total.';
        ';
      }
      
      echo'
      mana_distribution.setPlotData("Mana distribution", cmc_distribution);
      ';
      
  echo'
    </script>

    <table>
  ';

  foreach($types as $typename => $amount)
  {
    echo '
      <tr>
        <td>'.$typename.'</td>
        <td>'.$amount.'</td>
        <td>'.round($amount/$total_types * 100).'%</td>
      </tr>
          ';
  }

  echo '
      <tr>
        <th>Total</th>
        <th>'.$total_types.'</th>
        <th>100%</th>
      </tr>
    </table>
  ';
  
?>